<?php

/**
 * 资讯 API
 * User: don
 */

namespace app\modules\applets\controllers;

use yii;
use app\libs\ApiControl;
use app\modules\applets\models\Info;
use app\libs\GetData;
use app\libs\Pager;

class InfoController extends ApiControl
{
    public $layout = 'applets';
    public $enableCsrfValidation = false;

    /**
     * [actionIndex Index]
     * @Author don
     * @Date   2022-02-17
     * @return [type]     [description]
     */
    public function actionIndex()
    {
        $pagesize = 15;
        $page = Yii::$app->request->get('page', 1);
        $name = Yii::$app->request->get('name');
        $author = Yii::$app->request->get('author');
        $cate = Yii::$app->request->get('cate');
        $beginTime = strtotime(Yii::$app->request->get('beginTime'));
        $endTime = strtotime(Yii::$app->request->get('endTime'));
        $where = ' where 1=1';
        if ($name != false) {
            $where .= " and name like '%" . $name . "%'";
        }
        if ($author != false) {
            $where .= " and author= '" . $author . "'";
        }
        if ($cate != false) {
            $where .= " and cate=$cate";
        }
        if ($beginTime != false) {
            $where .= " and createTime>=$beginTime";
        }
        if ($endTime != false) {
            $where .= " and createTime<=$endTime";
        }
        $offset = $pagesize * ($page - 1);
        $count = Yii::$app->db->createCommand("select count(*) as count from {{%info_applets}} $where")->queryOne();
        $data = Yii::$app->db->createCommand("select id,name,sort,pic,keywords,summary,content,cate,createTime,count,viewCount from {{%info_applets}} $where order by id desc limit $offset,$pagesize")->queryAll();
        $url = "/applets/info/index?cate=$cate&name=$name&author=$author&beginTime=$beginTime&endTime=&endTime&page";
        $count = $count['count'];
        $page = new Pager("$url", $count, $page, $pagesize);
        $str = $page->GetPager();
        $cates = Info::$cate;
        $author = Info::find()->asArray()->groupBy('author')->where('author is not null or author !=""')->all();
        return $this->render('index', ['data' => $data, 'str' => $str, 'cates' =>$cates,'count'=>$count,'author'=>$author]);
    }

    /**
     * [actionAdd 添加或修改]
     * @Author don
     * @Date   2022-02-17
     * @return [type]     [description]
     */
    public function actionAdd()
    {
        if (!$_POST) {
            $id = Yii::$app->request->get('id', '');
            if (empty($id)) {
                return $this->render('add');
            } else {
                $data = Yii::$app->db->createCommand("select * from {{%info_applets}} where id=" . $id)->queryOne();
                return $this->render('add', ['data' => $data]);
            }
        } else {
            $getdata = new GetData();
            $must = array('name' => '标题', 'content' => '内容', 'cate' => '类别');
            $data = $getdata->PostData($must, 'info_applets');
            $arr = $getdata->Auto('createTime','viewCount');
            $data = array_merge($data, $arr);
            $data['pushTime'] = !empty($data['pushTime']) ? strtotime($data['pushTime']) : $data['createTime'];
            $data['startTime'] = !empty($data['startTime']) ? strtotime($data['startTime']) : '';
            $data['endTime'] = !empty($data['endTime']) ? strtotime($data['endTime']) : '';
            if (empty($data['id'])) {
                $re = Yii::$app->db->createCommand()->insert("{{%info_applets}}", $data)->execute();
            } else {
                $info = new Info();
                $re = $info->updateAll($data, 'id=:id', array(':id' => $data['id']));
            }
            if ($re) {
                $this->redirect('index');
            } else {
                echo '<script>alert("数据添加/修改失败，请重试");history.go(-1);</script>';
                die;
            }

        }

    }

    /**
     * [actionDel 删除]
     * @Author don
     * @Date   2022-02-17
     * @return [type]     [description]
     */
    public function actionDel()
    {
        $id = Yii::$app->request->get('id', '');
        $re = Info::deleteAll("id=:id", array(':id' => $id));
        if ($re) {
            die(json_encode(['code' => 1]));
        }
    }

}
